<?php

/**
 * @Project NUKEVIET 3.x - GETNEWS
 * @Author ĐẶNG ĐÌNH TỨ (dlinhvan@gmail.com)
 * @Copyright (C) 2010 webdep24.com All rights reserved
 * @Createdate 10/08/2012 08:00
 */

if( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );

if( ! defined( 'NV_IS_CRON' ) ) die( 'Stop!!!' );
// phan lay tin
function cron_auto_vnex( $url, $catid, $blockcat, $sourceid )
{
	global $db, $global_config, $db_config, $module, $folder, $module_config;

	//$url='http://nongnghiep.vn/nongnghiepvn/69/1/bnn.aspx';
	//$catids=1;
	//$blockcat=1;
	//$sourceid=0;
	$folder = date( "Y_m" );
	$module_name = "getnews";
	$module = $module_config[$module_name]['module'];
	$mod_data = str_replace( '-', '_', $module );
	$data = array();
	$link_arr = array();
	$data1 = array();
	$data2 = array();
	$error = "";
	$urlink = "http://vnexpress.net";
	$loop = 'div.folder-top';
	$title_t = 'p a.link-topnews';
	//$link_t = "div.cate_newsexcerpt p a";
	$home_t = "div.cate_newsexcerpt p";
	$img_t = "a img.img-topsubject";

	require_once ( NV_ROOTDIR . "/includes/class/geturl.class.php" );
	require_once ( NV_ROOTDIR . "/modules/getnews/global.function.php" );
	$getContent = new UrlGetContents( $global_config );
	$html = $getContent->get( $url );
	$html1 = str_get_html( nv_convert( $html ) );
	foreach( $html1->find( $loop ) as $e )
	{
		if( $e->find( $title_t, 0 ) ) $title = nv_unhtmlspecialchars( trim( $e->find( $title_t, 0 )->innertext ) );
		$alias = change_alias( $title );
		$data1[$alias]['title'] = $title;
		$data1[$alias]['alias'] = $alias;
		if( $e->find( $img_t, 0 ) )
		{
			$img = $e->find( $img_t, 0 )->src;
		}
		if( ! empty( $img ) )
		{
			if( nv_is_url( $img ) ) $data1[$alias]['homefile'] = $img;
			else  $data1[$alias]['homefile'] = $urlink . $img;
		}
		else  $data1[$alias]['homefile'] = "";
		if( $e->find( $title_t, 0 ) ) $link1 = $e->find( $title_t, 0 )->href;
		if( ! nv_is_url( $link1 ) ) $data1[$alias]['link'] = $urlink . $link1;
		else
		{
			unset( $data1[$alias] );
		}
	}
	sleep( 1 );
	foreach( $html1->find( 'div.folder-news' ) as $e )
	{
		if( $e->find( 'p a.link-title', 0 ) ) $title = change( $e->find( 'p a.link-title', 0 )->innertext );
		$alias = change_alias( $title );
		$data2[$alias]['title'] = $title;
		$data2[$alias]['alias'] = $alias;

		if( $e->find( 'a img.img-subject', 0 ) )
		{
			$img = $urlimages . $e->find( 'a img.img-subject', 0 )->src;
		}
		else  $img = "";
		if( ! empty( $img ) )
		{
			if( nv_is_url( $img ) ) $data2[$alias]['homefile'] = $img;
			else  $data2[$alias]['homefile'] = $urlink . $img;
		}
		else  $data2[$alias]['homefile'] = "";
		if( $e->find( 'p a.link-title', 0 ) ) $link2 = $e->find( 'p a.link-title', 0 )->href;
		if( ! nv_is_url( $link2 ) ) $data2[$alias]['link'] = $urlink . $link2;
		else
		{
			unset( $data2[$alias] );
		}
	}
	$html1->clear();
	unset( $html1, $html );
	$data = array_merge( $data1, $data2 );

	$array_block_cat_module = array();
	$sql = "SELECT bid, adddefault, title FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_block_cat` ORDER BY `weight` ASC";
	$result = $db->sql_query( $sql );
	while( list( $bid_i, $adddefault_i, $title_i ) = $db->sql_fetchrow( $result ) )
	{
		$array_block_cat_module[$bid_i] = $title_i;
	}
	$i = 1;
	foreach( $data as $alias => $array )
	{
		$sql = "SELECT COUNT(*) FROM `" . NV_PREFIXLANG . "_" . $mod_data . "_rows` WHERE `alias`=" . $db->dbescape_string( $alias );
		$result = $db->sql_query( $sql );
		list( $exist ) = $db->sql_fetchrow( $result );
		if( empty( $exist ) )
		{
			$html = $getContent->get( $array['link'] );
			$html1 = str_get_html( nv_convert( $html ) );
			if( ! ( $html1->find( 'div.content div', 0 ) ) )
			{
				$error .= "Lỗi cấu trúc dữ liệu website  đã bị thay đổi";
				$link_arr[] = $array['link'];
			}
			else
			{
				$a = "đasa";
				if( $html1->find( 'div.folder-header div.fr', 0 ) )
				{
					$time1 = $html1->find( 'div.folder-header div.fr', 0 )->innertext;
					$time2 = explode( 'GMT', $time1 );
					$time = nv_time( nv_conver_utf8( $time2[0] ) );
					$emin = substr( $time, -2, 2 );
					$ehour = substr( $time, -4, 2 );
					$y = substr( $time, -8, 4 );
					$d = substr( $time, 0, 2 );
					$m = substr( substr( $time, 0, -8 ), 2, 2 );
					if( $m < 10 ) $m = "0" . $m;
					else  $m = $m;
					$publtime = mktime( $ehour, $emin, 0, $m, $d, $y );
				}
				else
				{
					$error .= "Lỗi Không Cập Nhật Được Thời Gian";
					break;
				}
				$hometext = $html1->find( 'h2.Lead', 0 )->innertext;
				$hometext = preg_replace( '@<a[^>]*?.*?</a>@siu', ' ', strip_tags( $hometext ) );
				$array['hometext'] = preg_replace( '/[<>\/]/', '', $hometext );
				//if($html1 -> find('p[class="Normal" align="left"]', 0) -> outertext = "";
				if( $html1->find( 'div.flashContent"]', 0 ) ) $html1->find( 'span#FlashPlayer58048', 0 )->outertext = "";
				$html1->find( 'h1.Title', 0 )->outertext = "";
				$html1->find( 'p.nomal[align="right"]', 0 )->innertext = "";
				$html1->find( 'h2.Lead', 0 )->outertext = "";
				$html1->find( 'div.box-more', 0 )->outertext = "";
				$body = $html1->find( 'div.content div', 0 )->innertext;
				$bodytext = preg_replace( "'<script[^>]*>.*</script>'siU", '', $body );
				$bodytext = url_link( $bodytext, $urlink );
				if( $module_config['getnews']['load_image'] == 1 )
				{
					$bodytext = load_image( $bodytext, '' );
				}
				$bodytext = stripwhitespace( $bodytext );

				if( ! empty( $bodytext ) )
				{
					$imgfile = nv_get_images( $bodytext );
					if( ! empty( $imgfile ) and $module_config[$module_name]['image_body'] == 1 )
					{
						$img = explode( "/", $imgfile );
						$imgfile = end( $img );
						$imgfile = NV_UPLOADS_REAL_DIR . "/" . $module . "/" . $folder . "/" . $imgfile;
						if( file_exists( $imgfile ) )
						{
							$img_file = nv_filter( $imgfile );
							$homefile = $img_file[0];
							$homeimgthumb = $img_file[1];
						}
						elseif( ! empty( $array['homefile'] ) )
						{
							$homefile = $array['homefile'];
							$homeimgthumb = "";
						}
						elseif( ! empty( $imgfile ) )
						{
							$homefile = "";
							$homeimgthumb = "";
						}
					}
					elseif( ! empty( $imgfile ) )
					{
						$homefile = $imgfile;
						$homeimgthumb = "";
					}
					elseif( $array['homefile'] != "" )
					{
						$homefile = $array['homefile'];
						$homeimgthumb = "";

					}
					else
					{
						$homefile = "";
						$homeimgthumb = "";
					}

				}
				else
				{
					$error .= "Lỗi chi tiết bài viết trống";
					break;
				}

				if( empty( $error ) )
				{
					$error .= save_data_auto( $catid, $blockcat, $array['title'], $array['alias'], $array['hometext'], $bodytext, $sourceid, $publtime, $homefile, $homeimgthumb, $array['link'], $array_block_cat_module );
				}
				$html1->clear();
				unset( $html1, $html, $bodytext );
			}
		}
		$i++;
		if( $i > 10 ) break;
	}
	if( ! empty( $error ) )
	{
		nv_sendmail( array( $global_config['site_name'], $global_config['site_email'] ), $global_config['error_send_email'], "Error get new :" . $error, "Error get new :" . $error . " từ website " . NV_SERVER_NAME . " nguồn " . $url . "  " . implode( "<br />", $link_arr ) . "  " . $global_config['site_email'] );
	}
	else
	{
		optimize();
		nv_del_moduleCache( $module );
	}
	return true;
}

?>